<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    okText="确认"
    :class="editType =='detail'?'pic-preview':''"
    :cancelText="editType =='detail'?'关闭':'取消'">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="账户名">
          <a-input disabled placeholder="请输入账户名" v-decorator="['UserInfo_Wechat', {}]" />
        </a-form-item>
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="手机">
          <a-input disabled placeholder="请输入手机号" v-decorator="['Phone', {}]" />
        </a-form-item> -->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="所在公司">
          <a-input disabled placeholder="请输入所在公司" v-decorator="['UserInfo_Company', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="职务">
          <a-input disabled placeholder="请输入职务" v-decorator="['UserInfo_Job', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="换物名称">
          <a-input disabled placeholder="请输入换物名称" v-decorator="['ItemName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="币种类型">
          <a-input disabled v-decorator="['ItemType_Value', {}]" />
          <!-- <a-select disabled placeholder="请选择币种类型" v-decorator="['ItemType_Value', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in ItemTypeData">{{item.Value}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="寄出地址">
          <a-input placeholder="请输入换物名称" v-decorator="['Address', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="消费额度">
          <a-input disabled placeholder="请输入消费额度" v-decorator="['IntegralValue', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收件人">
          <a-input placeholder="请输入收件人" v-decorator="['Contacts', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="收件人手机号">
          <a-input placeholder="请输入收件人手机号" v-decorator="['Phone', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="快递公司">
          <a-input placeholder="请输入快递公司" v-decorator="['Express', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="快递号">
          <a-input placeholder="请输入快递号" v-decorator="['ExpressNo', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="寄出日期">
          <a-date-picker show-time placeholder="请输入寄出日期" v-decorator="['ExpressDate', {}]" />
        </a-form-item>
    
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "BaseInfoModal",
    watch:{
      visible(newVal,oldVal){
        if(newVal){
          this.editType = sessionStorage.getItem('editType');
        }
      }
    },
    data () {
      return {
        editType:'',
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        ItemType:{rules: [{ required: true, message: '请输入选择币种类型!' }]},
        ItemName:{rules: [{ required: true, message: '请输入物品名!' }]},
        Amt:{rules: [{ required: true, message: '请输入额度!' }]},
        value:{rules: [{ required: true, message: '请输入分类值!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态 Y-有效 N-无效!' }]},
        },
        ItemTypeData:[],
        url: {
          add: "/userExchange/add",
          edit: "/userExchange/update",
          baseList:'/baseInfo/queryRows'
        },
      }
    },
    created () {
      this.getBaseData();
    },
    methods: {
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'ItemType'
            }]}).then((res)=>{
          if(res.status == 1){
            this.ItemTypeData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'UserInfo_Wechat','UserInfo_UserName','UserInfo_Company','Job','Contacts','Address','ItemName','Phone','Express','ExpressDate','ExpressNo','IntegralValue','ItemType_Value','UserInfo_Job'))
      //时间格式化
      this.form.setFieldsValue({ExpressDate:this.model.ExpressDate?moment(this.model.ExpressDate,'YYYY-MM-DD HH:mm:ss'):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.ExchangeId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.ExpressDate = formData.ExpressDate?formData.ExpressDate.format('YYYY-MM-DD HH:mm:ss'):null;
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>